<template>
	<view class="container">
		<view class="top-box">
			<image :src="pageBg" class="pageBg" v-if="pageBg"></image>
			<view class="nav-box">
				<view class="nav-left">
					<!-- <image src="../../static/message/arrow-left.png" class="arrow-left" @click="navigatorBack">
					</image> -->
					<view class="user-box">
						<image src="../../static/logo.png" class="user-avatar" mode="aspectFill" @click="showUser=true">
						</image>
						<view class="user-info">
							<view class="name">
								<text>AI-小缘</text>
							</view>
						</view>
					</view>
				</view>
				<view class="nav-right"></view>
			</view>
		</view>
		<view class="content">
			<z-paging ref="paging" class="msg-list" v-model="msgList" :auto="false" @refresherTouchend="queryList"
				:refresher-enabled="false" :loading-more-enabled="false">
				<view class="row" v-for="(item,index) in msgList" :key="index" :id="'msg'+item.id">
					<!-- 自己发出的消息 -->
					<view class="my" v-if="item.sender=='me'">
						<!-- 左-消息 -->
						<view class="left">
							<!-- 文字消息 -->
							<view class="bubble">
								<rich-text :nodes="item.message"></rich-text>
							</view>
						</view>
						<!-- 右-头像 -->
						<view class="right">
							您
							<!-- <image src="../../static/logo.png" mode="aspectFill"></image> -->
						</view>
					</view>
					<!-- 别人发出的消息 -->
					<view class="other" v-if="item.sender=='you'">
						<!-- 左-头像 -->
						<view class="left">
							<image src="../../static/logo.png" mode="aspectFill"></image>
						</view>
						<!-- 右-用户名称-时间-消息 -->
						<view class="right">
							<view class="username">
								<view class="name">AI-小缘</view>
								<!-- <view class="time">{{item.createtime}}</view> -->
							</view>
							<!-- 文字消息 -->
							<view class="bubble">
								<rich-text :nodes="item.message"></rich-text>
							</view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 底部输入栏 -->
		<view class="input-box">
			<view class="textbox">
				<view class="text-mode">
					<view class="box">
						<textarea auto-height="true" v-model="textMsg" />
					</view>
				</view>
			</view>
			<view class="send"  style="margin-right: 32rpx;">
				<view class="btn" v-if="!loading" @tap="sendAiMessage">发送</view>
				<view class="btn" v-if="loading">•••</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				pageBg: '/static/common/page_bg2.png',
				//文字消息
				textMsg: '',
				msgList: [],
				loading: false,
			};
		},
		onLoad(option) {

		},
		onShow() {},
		methods: {
			sendAiMessage() {
				if (!this.textMsg||this.loading) {
					return;
				}
				this.loading = true
				// 保存当前的消息内容
				const messageToSend = this.textMsg;
				this.msgList.push({
					sender: 'me',
					message: this.textMsg,
				})
				this.scrollToBottom()
				this.textMsg = '';
				this.$Api.common.sendAiMessage({
					messages: messageToSend
				}).then(res => {
					this.loading = false
					this.msgList.push({
						sender: 'you',
						message: res.data.result,
					})
					this.scrollToBottom()
					
				}).catch(error=>{
					this.loading = false
					this.msgList.push({
						sender: 'you',
						message: '服务器错误请重试',
					})
					this.scrollToBottom()
				})
			},
			scrollToBottom() {
				if (this.$refs.paging) {
					this.$refs.paging.scrollToBottom(false)
				}
			},
			queryList() {
				if (this.$refs.paging) {
					this.$refs.paging.complete(this.msgList)
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import "@/static/HM-chat/css/style.scss";

	.contentBox {
		width: 90vw;
		min-height: 440rpx;
		box-sizing: border-box;
		padding: 38rpx 44rpx 82rpx;
		box-shadow: 0rpx 4rpx 32rpx 0rpx #FFF0FD;
		border-radius: 32rpx;
		background: linear-gradient(180deg, #FFDAF5 0%, #FFFFFF 20%);
		position: relative;
		text-align: center;

		.popup-close {
			position: absolute;
			right: 0;
			top: -120rpx;
			width: 88rpx;
			height: 88rpx;
		}

		.popup-image {
			margin-top: 60rpx;
			width: 412rpx;
			height: 316rpx;
		}

		.popup-title {
			font-weight: 600;
			font-size: 34rpx;
			color: #323334;
			text-align: center;
		}


		.popup-tips {
			margin-top: 18rpx;
			font-size: 28rpx;
			color: #9A9A9A;
		}

		.button-group {
			margin-top: 68rpx;

			.title {
				display: block;
				color: #ffffff;
				text-decoration: none;
			}

			.button1 {
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 auto;
				width: 600rpx;
				height: 88rpx;
				font-weight: bold;
				font-size: 30rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 88rpx;
				background: linear-gradient(135deg, #FE93BF 0%, #A27AFF 100%);
				border-radius: 44rpx;
			}
		}
	}

	.container {
		width: 100%;
		min-height: 100vh;
		position: relative;
		background-color: #f8f8f8;

		.top-box {
			width: 100%;
			position: fixed;
			z-index: 100;

			.pageBg {
				position: absolute;
				top: 0;
				width: 100%;
				height: 120rpx;
			}

			.waring-text {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 20rpx 0;
				background-color: #FFE9FC;
				font-size: 24rpx;
				color: #F169F6;
			}

			.nav-box {
				padding: 20rpx;
				position: relative;
				z-index: 10;
				display: flex;
				justify-content: space-between;
				align-content: center;
				font-size: 28rpx;
				color: #323334;

				.nav-left,
				.nav-right {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.arrow-left {
						width: 24rpx;
						height: 48rpx;
					}

					.unlock {
						width: 148rpx;
						height: 58rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						background: #28C445;
						border-radius: 44rpx;
						font-size: 30rpx;
						color: #FFFFFF;

						.icon_wx {
							width: 38rpx;
							height: 32rpx;
							margin-right: 8rpx;
						}
					}

					.more {
						width: 12rpx;
						height: 40rpx;
						margin-left: 42rpx;
					}

					.user-box {
						display: flex;
						align-items: center;

						.user-avatar {
							width: 70rpx;
							height: 70rpx;
							border-radius: 50%;
							margin-right: 10rpx;

						}

						.user-info {
							font-size: 24rpx;
							color: #9A9A9A;

							.name {
								font-weight: bold;
								font-size: 28rpx;
								color: #323334;
							}

							.detail {
								margin-top: 10rpx;
								letter-spacing: 4rpx;
							}
						}
					}
				}
			}

		}
	}
</style>